package initialize

import (
	"fresh-shop-gin/config"
	"fresh-shop-gin/middleware"
	"fresh-shop-gin/router"

	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

func InitRouter(cfg *config.Config) {

	r := gin.New()

	// 全局中间件
	r.Use(middleware.ZapLogger())
	r.Use(middleware.GinLogger())
	r.Use(middleware.ZapRecovery())
	r.Use(middleware.Cors())

	// 静态文件
	r.Static("/static", "./static")

	// swagger文档
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	// 路由组
	systemRouter := router.RouterGroupApp.System

	// 公共路由组
	publiGroup := r.Group("/api")
	{
		systemRouter.UserRouter.InitUserLogin(publiGroup)
	}

	// 私有路由组
	privateGroup := r.Group("/api")
	privateGroup.Use(middleware.JWTAuthMiddleware())
	{
		systemRouter.UserRouter.InitUserPrivate(privateGroup)
	}

	r.Run(cfg.Server.Port)
}
